package nextEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"strconv"
)

type TagEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	TagName string // 分片

	Value map[string]interface{} // 配置参数
}

type tagPo struct {
	Id         int64 `json:"id" db:"id"`                  // 对象id
	CreateTime int64 `json:"createTime" db:"create_time"` // 创建时间
	UpdateTime int64 `json:"updateTime" db:"update_time"` // 更新时间

	TagName string `json:"TagName" db:"tag_name"`

	Value map[string]interface{} `json:"value" db:"value"`
}

func (e *TagEntity) Type() string {
	return "TagEntity"
}

func (e *TagEntity) TableName() string {
	return "tb_tag_xx"
}

func (e *TagEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *TagEntity) Instance() *TagEntity {
	return &TagEntity{
		BaseEntity: new(commEntity.BaseEntity),
		Value:      make(map[string]interface{}),
	}
}

func (e *TagEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *TagEntity) NewPo() interface{} {
	return &tagPo{
		Value: make(map[string]interface{}),
	}
}

func (e *TagEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *TagEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, strconv.FormatUint(uint64(e.BaseEntity.Id), 10))
	return p
}

func (e *TagEntity) BuildByJson(jsn *string) error {
	var po tagPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *TagEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *TagEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*tagPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime

	ptr.Value = po.Value
	ptr.TagName = po.TagName
}

func (e *TagEntity) BuildToPo() interface{} {
	ptr := e
	po := tagPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime

	po.Value = ptr.Value
	po.TagName = ptr.TagName

	return &po
}
